using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class admin_CustomerBookingManagement : admin_BaseUserControl
{
    CustomBooking cb = new CustomBooking();
    CustomBookingDAO daoCusBoo = new CustomBookingDAO();
    protected void Page_Load(object sender, EventArgs e)
    {
        ActiveAccording = "booking"; //set active task menu
        LoadData();

    }
    /// <summary>
    /// Load initialize data
    /// </summary>
    public void LoadData()
    {
        cmbState.Items.Add(new ListItem("Booked","0"));
        cmbState.Items.Add(new ListItem("Accepted", "1"));
        cmbState.Items.Add(new ListItem("Canceled", "2"));
        try
        {
            int bid = int.Parse(Request["id"]);
            CustomBookingDAO daoCB = new CustomBookingDAO();
            BookingDAO daoBooking=new BookingDAO();
            FacilityDAO daoFac = new FacilityDAO();
            LocationDAO daoLoc=new LocationDAO();
            PersonDAO daoPerson = new PersonDAO();            
            Person per = new Person();
            Booking bk = new Booking();            
            cb = daoCB.GetCustomBookingByBID(bid);
            bk = daoBooking.GetBooking(bid);
            cb.CustomerID = bk.CustomerID;
            cb.BookingID = bk.BookingID;
            cb.State = bk.State;            
            int depLocId = cb.DeparturePlace.Id;
            int arvLocId = cb.ArrivalPlace.Id;
            cb.DeparturePlace = daoLoc.GetLocation(depLocId);
            cb.ArrivalPlace=daoLoc.GetLocation(arvLocId);
            cb.Facility = daoFac.GetFacility(cb.Facility.Id);            
            per = daoPerson.GetPerson(cb.CustomerID);
            
            lblCustomerName.Text = per.FirstName + " " + per.LastName;
            lblDateOfBirth.Text = per.DateOfBirth.ToString("MMM, dd yyyy");
            lblAddress.Text = per.Address;
            lblEmail.Text = per.Email;
            lblPhone.Text = per.Phone;
            lblDeparturePlace.Text = cb.DeparturePlace.Name;
            lblArrivalPlace.Text = cb.ArrivalPlace.Name;
            txtDepartureDate.Text = cb.DepartureTime.ToString("MM/dd/yyyy");
            txtGuestNumber.Text = cb.GuestNumber.ToString();
            txtNote.Text = cb.Note.ToString();
            lblFacility.Text = cb.Facility.Name;
            cmbState.SelectedIndex = (int)cb.State;
            txtPrice.Text = cb.Price.ToString("#0.#");
        }
        catch (Exception ex)
        {
            ShowMessage("Error. Data could not be loaded");
        }
    }
   
    protected void lnkUpdate_Click(object sender, EventArgs e)
    {
        BookingDAO daoBooking = new BookingDAO();
        cb.GuestNumber =int.Parse( txtGuestNumber.Text);       
        cb.DepartureTime =DateTime.Parse(txtDepartureDate.Text);         
        cb.State =(byte)cmbState.SelectedIndex;
        cb.Price = decimal.Parse(txtPrice.Text);
        cb.Note = txtNote.Text;
        
        if (daoCusBoo.Update(cb) > 0)
        {
            if (daoBooking.UpdateState(cb.BookingID, cb.State) > 0)
                ShowMessage("Updated successfully");
            else ShowMessage("Error occured. Booking state could not be changed");
        }
        else ShowMessage("Error: Error occured when trying to update booking  information");
    }
    protected void Delete_Click(object sender, EventArgs e)
    {
        BookingDAO daoBooking = new BookingDAO();
        if (daoBooking.Delete(cb.BookingID) > 0)
        {
            ShowMessage("Booking  has been removed");
            lnkDelete.Visible = false;
            lnkUpdate.Visible = false;    
        }
        else ShowMessage("Error has occured when trying to remove current CustomerBooking");
    }

    /// <summary>
    /// Return request string for search in list page
    /// </summary>
    public string GetSearchRequest()
    {
        if (Request["search"] != null) return Request["search"];
        return "";
    }
}

